<template>
  <div class="login">
    <div class="login-left">
      <div class="footer">
      <span>主办单位名称：浦江慧农文化发展有限公司</span>
      <span>技术支持：安校科技（杭州）有限公司</span>
      <span>
        网站备案号：
        <a href="https://beian.miit.gov.cn/#/Integrated/index">浙ICP备2025174033号-1</a>
      </span>
    </div>
    </div>

    <div class="login-right">
      <div class="login-right-code-icon"></div>
      <div class="login-right-main">
        <div class="login-right-main-title">研学·浦江</div>
        <div class="login-right-main-subtitle">人纸合会，龙舞擂马，乱弹板凳龙</div>
        <div class="login-right-main-subtitle">笔绘安时，竹雕麦贴，什锦唱丰年</div>
        <el-form ref="formRef" style="margin-top: 60px;" label-width="80px" :status-icon="true" :model="login"
          :rules="rules" label-position="top" @keyup.enter="onLogin">
          <el-form-item prop="username">
            <el-input v-model="login.username" placeholder="请输入账号" prefix-icon="user" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input placeholder="请输入密码" v-model="login.password" prefix-icon="lock" autocomplete="off"
              show-password></el-input>
          </el-form-item>
          <el-form-item prop="captcha">
            <el-space class="login-right-main-code">
              <el-input v-model="login.captcha" placeholder="请输入验证码" prefix-icon="first-aid-kit"></el-input>
              <img style="vertical-align: middle; height: 30px; cursor: pointer" :src="state.captchaUrl"
                @click="onRefreshCode" alt="" />
            </el-space>
          </el-form-item>
          <el-form-item label-width="0">
            <el-button size="small" :disabled="state.loading" @click="onLogin" class="login-right-main-btn"> 登录
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from "vue";
import { CacheToken } from "@/constants/cacheKey";
import baseService from "@/service/baseService";
import { setCache } from "@/utils/cache";
import { ElMessage } from "element-plus";
import { getUuid } from "@/utils/utils";
import app from "@/constants/app";
import SvgIcon from "@/components/base/svg-icon/index";
import { useAppStore } from "@/store";
import { useRouter } from "vue-router";

const store = useAppStore();
const router = useRouter();

const state = reactive({
  captchaUrl: "",
  loading: false,
  year: new Date().getFullYear()
});

const login = reactive({ username: "", password: "", captcha: "", uuid: "" });

onMounted(() => {
  //清理数据
  store.logout();
  getCaptchaUrl();
});
const formRef = ref();

const rules = ref({
  username: [{ required: false, message: "必填项不能为空", trigger: "blur" }],
  password: [{ required: false, message: "必填项不能为空", trigger: "blur" }],
  captcha: [{ required: false, message: "必填项不能为空", trigger: "blur" }]
});

const getCaptchaUrl = () => {
  login.uuid = getUuid();
  state.captchaUrl = `${app.api}/captcha?uuid=${login.uuid}`;
};

const onRefreshCode = () => {
  getCaptchaUrl();
};

const onLogin = () => {
  formRef.value.validate((valid: boolean) => {
    if (valid) {
      state.loading = true;
      baseService
        .post("/login", login)
        .then((res) => {
          state.loading = false;
          if (res.code === 0) {
            setCache(CacheToken, res.data, true);
            ElMessage.success("登录成功");
            router.push("/");
          } else {
            ElMessage.error(res.msg);
          }
        })
        .catch(() => {
          state.loading = false;
          onRefreshCode();
        });
    }
  });
};
</script>

<style lang="less">
.login {
  .el-form-item__label {
    color: #fff;
  }

  .el-input__wrapper {
    background-color: transparent;
  }

  .el-input__inner {
    border-width: 0;
    border-radius: 12px;
    background-color: transparent;
    color: #fff;
  }
}
</style>

<style lang="less" scoped>
@import url("@/assets/theme/base.less");

.login {
  width: 100vw;
  height: 100vh;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;

  &-left {
    justify-content: center;
    flex-direction: column;
    background-image: url('@/assets/images/login-bg1.png');
    background-size: cover;
    color: #fff;
    height: 90%;
    width: 66%;

    &-title {
      text-align: center;
      color: #fff;
      font-weight: 300;
      letter-spacing: 2px;
      font-size: 32px;
    }

    .footer {
      width: 100%;
      position: absolute;
      bottom: -50px;
      color: #999;
      font-size: 13px;
      display: flex;
      justify-content: center;
      width: 100%;

      span {
        padding: 20px;
      }

      a {
        color: #999;
        text-decoration: none;
      }
    }
  }

  &-right {
    border-left: none;
    color: #fff;
    background-image: url('@/assets/images/login-bg2.png');
    background-size: cover;
    height: 100%;
    width: 34%;

    &-code-icon {
      background-image: url('@/assets/images/login-code-icon.png');
      background-size: contain;
      background-repeat: no-repeat;
      width: 80px;
      height: 80px;
      position: absolute;
      top: 20px;
      right: 20px;
    }

    &-main {
      margin: 0 auto;
      width: 65%;

      &-title {
        color: #fff;
        margin-bottom: 20px;
        font-weight: 600;
        font-size: 40px;
      }

      &-subtitle {
        color: #fff;
        font-weight: 400;
        font-size: 17px;
      }

      &-lang .iconfont {
        font-size: 20px;
        color: #606266;
        font-weight: 800;
        width: 20px;
        height: 20px;
      }

      &-code {
        width: 100%;

        .el-space__item:first-child {
          flex: 1;
        }
      }

      &-btn {
        width: 100%;
        height: 40px;
        letter-spacing: 2px;
        cursor: pointer;
        margin-top: 60px;
        font-family: neo, sans-serif;
        transition: 0.25s;
        color: #0E4373;
        font-size: 15px;
      }
    }
  }

  .login-footer {
    text-align: center;
    position: absolute;
    bottom: 0;
    padding: 20px;
    color: rgba(255, 255, 255, 0.6);

    p {
      margin: 10px 0;
    }

    a {
      padding: 0 5px;
      color: rgba(255, 255, 255, 0.6);

      &:focus,
      &:hover {
        color: #fff;
      }
    }
  }

  &-left,
  &-right {
    position: relative;
    min-height: 500px;
    align-items: center;
    display: flex;
  }
}
</style>
